package com.heking.securityapp;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.heking.security.LogUtils;
import com.heking.security.SecurityUtils;
import com.heking.security.webview.WebPageLauncher;
import com.heking.securityapp.pages.BundleWrapActivity;
import com.heking.securityapp.pages.EncryptActivity;
import com.heking.securityapp.pages.FileManagerActivity;
import com.heking.securityapp.pages.SPActivity;
import com.heking.securityapp.pages.SensitiveActivity;
import com.heking.securityapp.pages.SslCheckActivity;
import com.heking.securityapp.pages.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = ">>>MainActivity";
    private static final String SIGNATURE ="D89C1B2FAF35977D65DFCB1CCB138515";
    private static final String SIGNATURE1 ="079899B186F17F4037BB94E7743640C2";
    private ListAdapter mListAdapter;
    private RecyclerView mRecyclerView;

    private List<Menu> getData() {
        List<Menu> list = new ArrayList<>();
        list.add(new Menu(1, "WebView"));
        list.add(new Menu(2, "Bundle", BundleWrapActivity.class));
        list.add(new Menu(3, "解密", EncryptActivity.class));
        list.add(new Menu(4, "SharedPreferencesWrap", SPActivity.class));
        list.add(new Menu(5, "SSL", SslCheckActivity.class));
        list.add(new Menu(6, "脱敏", SensitiveActivity.class));
        list.add(new Menu(7, "文件管理工具", FileManagerActivity.class));
        return list;
    }

    private ListAdapter.OnItemClickListener onItemClickListener = (listAdapter, position) -> {
        Menu menu = listAdapter.getData().get(position);
        Class<? extends Activity> navigation = menu.navigation;
        if (navigation != null) {
            startActivity(new Intent(this, navigation));
        } else {
            switch (menu.id) {
                case 1:
                    WebPageLauncher launcher = new WebPageLauncher()
                            .setUrl("https://www.baidu.com")
//                            .setContentSource("的设计费胜多负少")
//                            .setDefaultFontSize(getResources().getDimensionPixelOffset(R.dimen.s_14sp))
                            .setTitle("测试Web");
                    launcher.start(this, WebViewActivity.class);
                    break;


                default:

                    break;
            }

        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        mListAdapter = new ListAdapter(getData(), this);
        mListAdapter.setOnItemClickListener(onItemClickListener);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mListAdapter);
        String[] permissions=new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
        boolean granted=true;
        for (String p : permissions) {
             if(ActivityCompat.checkSelfPermission(this,p)!= PackageManager.PERMISSION_GRANTED){
                 granted=false;
             }
        }
        if(!granted){
            ActivityCompat.requestPermissions(this, permissions, 1000);
        }
        if(SecurityUtils.checkSignature(this,SIGNATURE)){
            Toast.makeText(this, "签名校验成功", Toast.LENGTH_SHORT).show();
            LogUtils.d(TAG, "签名校验成功:"+SIGNATURE);
        }else{
            Toast.makeText(this, "签名校验失败", Toast.LENGTH_SHORT).show();
            LogUtils.d(TAG, "签名校验失败:"+SIGNATURE);

        }
        if(SecurityUtils.checkSignature(this,SIGNATURE1)){
            Toast.makeText(this, "签名校验成功", Toast.LENGTH_SHORT).show();
            LogUtils.d(TAG, "签名校验成功:"+SIGNATURE1);
        }else{
            LogUtils.d(TAG, "签名校验失败:"+SIGNATURE1);

        }

    }


    private void findViews() {
        mRecyclerView = findViewById(R.id.recyclerView);
    }

    private static class Menu {
        private int id;
        private String name;
        private Class<? extends Activity> navigation;
        private Bundle args;

        public Menu(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public Menu(int id, String name, Class<? extends Activity> navigation) {
            this.id = id;
            this.name = name;
            this.navigation = navigation;
        }

        public Menu(int id, String name, Class<? extends Activity> navigation, Bundle args) {
            this.id = id;
            this.name = name;
            this.navigation = navigation;
            this.args = args;
        }
    }


    private static class ListAdapter extends RecyclerView.Adapter<ListAdapter.VH> implements View.OnClickListener {
        private List<Menu> data;
        private Context context;
        private OnItemClickListener onItemClickListener;

        public ListAdapter(List<Menu> data, Context context) {
            this.data = data;
            this.context = context;
        }

        public void setData(List<Menu> data) {
            this.data = data;
        }

        public List<Menu> getData() {
            return data;
        }


        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new VH(LayoutInflater.from(context).inflate(R.layout.simple_list_item, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull VH holder, int position) {
            Menu s = data.get(position);
            holder.textView.setText(s.name);
            holder.itemView.setClickable(true);
            holder.itemView.setTag(position);
            holder.itemView.setOnClickListener(this);
        }

        @Override
        public int getItemCount() {
            if (data == null) {
                return 0;
            }
            return data.size();
        }

        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag();
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(this, pos);
            }

        }

        private static final class VH extends RecyclerView.ViewHolder {
            private TextView textView;

            public VH(@NonNull View itemView) {
                super(itemView);
                textView = (TextView) itemView;
            }
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }

        private interface OnItemClickListener {
            void onItemClick(ListAdapter listAdapter, int position);
        }
    }

}